/*

*/

hotkey
	icon = 'stuff.dmi'
	icon_state="hotkey"
	parent_type=/obj

	layer=MOB_LAYER+9

	var
		spell/cast
		book/book
		locked
		friendly

	New(book/b)
		..()
		book = b

	proc
		set_spell(spell/s)

			cast = s ? s : null
			name = s ? s.name : initial(name)

			reset_overlays()

		set_lock(bool)
			locked = bool
			reset_overlays()

		set_friendly(bool)
			if(!cast)return
			cast.hits_friendly = bool
			friendly = bool
			reset_overlays()

		reset_dragging(player/p)
			p.client.mouse_pointer_icon = null
			p.client.dragging = null

		reset_overlays()
			overlays = new/list()
			if(cast)	overlays += cast
			if(locked)	overlays += new /obj/overlay/locked
			if(friendly)overlays += new /obj/overlay/friendly

	// Cast a spell
	LeftClick(player/clicker, loc, control, params)
		if(cast)
			cast.LeftClick(clicker,loc,control,params)

	// Unlock a spell
	RightClick(player/clicker, loc, control, params)
		set_lock(!locked)

	// Unlock a spell
	LeftCtrlClick(player/clicker, loc, control, params)
		RightClick(clicker, loc, control, params)

	// Toggle friendly spell usage
	LeftShiftClick(player/clicker, loc, control, params)
		LeftAltClick(clicker, loc, control, params)

	// Toggle friendly spell usage
	LeftAltClick(player/clicker, loc, control, params)
		set_friendly(!friendly)

	MouseDrag(over_object,src_location,over_location,src_control,over_control,params)
		if(cast && !locked)
			usr.client.mouse_pointer_icon = icon(cast.icon, cast.icon_state)
			usr.client.dragging = new /draginfo(cast, src, book, usr)

	MouseDrop(over_object,src_location,over_location,src_control,over_control,params)
		var/draginfo/d = usr.client.dragging
		if(!d)return
		if(ishotkey(over_object))
			var/hotkey/h = over_object

			if(h.locked)
				reset_dragging(d.player)
				return

			if(h.cast)
				var/spell/newspell = h.cast

				var/oldpos = d.book.spells.Find(d.spell)
				var/newpos = d.book.spells.Find(h.cast )
				var/spell/oldspell = d.book.spells[oldpos]

				d.book.spells[oldpos] = d.book.spells[newpos]
				d.book.spells[newpos] = oldspell

				h.set_spell(d.spell)
				d.hotkey.set_spell(newspell)

				d.hotkey.set_friendly(h.friendly)
				h.set_friendly(d.spell.hits_friendly)

			else
				d.book.spells[d.player.hotkeys.Find(h)] = d.spell
				d.book.spells[d.player.hotkeys.Find(d.hotkey)] = null

				h.set_spell(d.spell)
				d.hotkey.set_spell(null)

				d.hotkey.friendly = 0
				d.hotkey.reset_overlays()

				h.set_friendly(d.spell.hits_friendly)

		reset_dragging(d.player)

